import modelExtend from 'dva-model-extend'
import {
  query,
  pay,
  show,
  changeList,
  cancel,
  payBatch
} from 'services/contractOrders'
import {lwjPageModel} from 'utils/model'
import {routerRedux} from 'dva/router'
import {Modal, Button} from 'antd'
import {getRegionCodes} from 'utils/utils';

export default modelExtend(lwjPageModel, {
  namespace: 'contractOrders',
  state: {
    items: [],
    pagination: {},
    currentItem: {},
    payModalVisible: false,
    payConfirmModalVisible: false,
    pageOptions: {
      sort: 'createTime',
      order: 'DESC'
    },
    queryParam: {
      queryUnpaidContract: true // 获取待缴尾款>0的数据
    },
    contractSelectedRowKeys: [],
    selectedUnpaid: [],
    payBatchVisible: false
  },
  subscriptions: {
    setup({dispatch, history}) {
      history.listen(({pathname}) => {
        /**
         * 列表页的监听
         */
        if (pathname === '/order/contractOrders') {
          dispatch({type: 'query', payload: {}})
        }
      })
    }
  },

  effects: {
    * query({
      payload
    }, {call, put, select}) {
      yield put({
        type: 'asyncData',
        payload: {
          ...payload
        }
      })
      const pageOptions = yield select(({contractOrders}) => contractOrders.pageOptions)
      const queryParam = yield select(({contractOrders}) => contractOrders.queryParam)
      queryParam.queryUnpaidContract = true
      const queryPayload = {
        ...pageOptions,
        queryParam: queryParam
      }

      const data = yield call(query, queryPayload)

      yield put({
        type: 'querySuccess',
        payload: {
          ...data
        }
      })
    },

    * changeList({
      payload
    }, {call, put, select}) {
      yield put({
        type: 'asyncData',
        payload: {
          ...payload
        }
      })
      const pageOptions = yield select(({contractOrders}) => contractOrders.pageOptions)
      const queryParam = yield select(({contractOrders}) => contractOrders.queryParam)

      const queryPayload = {
        ...pageOptions,
        queryParam: queryParam
      }
      const data = yield call(changeList, queryPayload)

      yield put({
        type: 'querySuccess',
        payload: {
          ...data
        }
      })
    },

    * onShow({
      payload
    }, {call, put}) {
      const {id} = payload
      const res = yield call(show, id)
      let data = res.result
      // 拼装地址信息 const province = data.consigneeProvince || {} const city =
      // data.consigneeCity || {} const district = data.consigneeDistrict || {}
      data.consigneeRegion = getRegionCodes(data.consigneeProvince, data.consigneeCity, data.consigneeDistrict)

      if (res.success) {
        yield put({type: 'showSuccess', payload: data})
      }
    },

    * onCancel({
      payload
    }, {call, put}) {
      const {id, version} = payload
      const res = yield call(cancel, id, version)

      if (!res.success) {
        throw new Error(res.message)
      }

    },

    * payBatch({
      payload
    }, {call, put}) {
      const res = yield call(payBatch, payload)

      if (!res.success) {
        throw new Error(res.message)
      }
      yield put(routerRedux.push(`/order/contractOrders`));

    },

    * selectedUnpaid({
      payload
    }, {call, put, select}) {
      const arr = payload.selectedRows
      let selectedUnpaid = [];
      arr.forEach(element => {
        selectedUnpaid.push({id: element.id, version: element.version})
      });
      yield put({
        type: 'setSelectedUnpaid',
        payload: {
          selectedUnpaid: selectedUnpaid
        }
      })
    },

    * onPay({
      payload
    }, {call, put}) {
      const {
        currentItem: {
          id,
          version
        },
        queryParam
      } = payload
      const res = yield call(pay, id, version)

      if (res.success) {
        yield put({type: 'query', payload: queryParam})
        yield put({type: 'hideModal', payload: {}})
      } else {
        Modal.error({title: '失败', content: res.message})
      }
    }
  },

  reducers: {
    clearItem(state, {payload}) {
      state.currentItem = {}

      return {
        ...state
      }
    },

    setContractSelectedRowKeys(state, {payload}) {
      state.contractSelectedRowKeys = payload

      return {
        ...state
      }
    },

    isShowPayBatchModal(state, {payload}) {
      return {
        ...state,
        payBatchVisible: payload
      }
    },

    /**
     * 显示支付的modal
     * @param state
     * @param payload
     * @returns {{}}
     */
    showPayModal(state, {payload}) {
      state.payModalVisible = true

      return {
        ...state
      }
    },

    setSelectedUnpaid(state, {payload}) {
      return {
        ...state,
        selectedUnpaid: payload.selectedUnpaid
      }
    },

    /**
     * 显示确定的Modal
     * @param state
     * @param payload
     * @returns {{}}
     */
    showPayConfirmModal(state, {payload}) {
      state.payConfirmModalVisible = true

      return {
        ...state
      }
    },

    /**
     * 隐藏所有modal
     * @param state
     * @param payload
     * @return {{}}
     */
    hideModal(state, {payload}) {
      state.payModalVisible = false
      state.payConfirmModalVisible = false

      return {
        ...state
      }
    },

    clearContractSelectedRowKeys(state, {payload}) {
      state.contractSelectedRowKeys = []

      return {
        ...state
      }
    }
  }
})
